package com.czh.subject.domain.handler.subject;

import com.czh.subject.common.enums.SubjectTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目类型工厂
 * @author caozaihui
 * @version 1.0
 * @since 2024/7/31 下午 3:04
 */
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {

    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlerList;
    private final Map<SubjectTypeEnum,SubjectTypeHandler> subjectTypeHandlerMap = new HashMap<>();
    public SubjectTypeHandler getSubjectTypeHandler(int subjectTypeCode) {
        return subjectTypeHandlerMap.get(SubjectTypeEnum.getType(subjectTypeCode));
    }

    /**
     * Spring装载完Bean之后要执行的逻辑
     * @since 2024/7/31 下午 3:22
     */
    @Override
    public void afterPropertiesSet()  {
        subjectTypeHandlerList.forEach(
                subjectTypeHandler -> subjectTypeHandlerMap.put(subjectTypeHandler.getHandlerType(),subjectTypeHandler));
    }
}
